package cn.openread.es;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.nio.charset.StandardCharsets;

/**
 * 用途：一切为了业务
 * <p>
 * Created by simon on 2020/3/8.
 */
public class JsonKit {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static String toJsonString(Object object) {
        try {
            String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
            return jsonString;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json.getBytes(StandardCharsets.UTF_8), clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
